package com.mx.zb.mng.ctrl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.bo.Operator;
import com.mx.zb.mng.cache.OperatorCaches;
import com.mx.zb.mng.dao.OperatorMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.Crypt;
import com.mx.zb.mng.utils.UnionCode;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;

/**
 * 员工模块
 * @author 张衡
 *
 */
@RestController
@RequestMapping("/a/sys")
public class OpertorRestCtrl {
	
	@Autowired
	OperatorMapper operatorMapper;
	
	@Autowired
	OperatorCaches operatorCaches;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	/**
	 * 添加员工
	 * @param o
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/operator"}, method= {RequestMethod.PUT})
	public AjaxResult save(@RequestBody Operator o) {
		Operator operator=operatorMapper.selectByName( o.getLoginName(), null);
		if (null!=operator) {
			return new AjaxResult().failure("用户工号不能重复");
		}
		String enPass= Crypt.enAes(o.getPassword());
		o.setPassword(enPass);
		o.setId(UnionCode.getShortUuid());
		o.setCreateDate(new Date());
		o.setCreateOper(ContextThread.get().getOperatorId());
		o.setAgentId(ContextThread.get().getAgentId());
//		o.setTenantCode(ContextThread.get().getTenantCode());
		operatorMapper.insert(o);
		return new AjaxResult().success();
	}
	/**
	 * 修改员工
	 * @param o
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/operators"}, method= {RequestMethod.POST})
	public AjaxResult update(@RequestBody Operator o) {
		Operator operator=operatorMapper.selectByName( o.getLoginName(), o.getId());
		if (null!=operator) {
			return new AjaxResult().failure("用户工号不能重复");
		}
		o.setUpdateDate(new Date());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		operatorMapper.updateByPrimaryKeySelective(o);
		operatorCaches.refreshRole(o.getId());
		return new AjaxResult().success();
	}
	/**
	 * 删除员工
	 * @param o
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/operator"}, method= {RequestMethod.DELETE})
	public AjaxResult delete(@RequestBody Operator o) {		
		operatorMapper.deleteByPrimaryKey(o.getId());
		return new AjaxResult().success();
	}
	/**
	 * 员工列表
	 * @return
	 */
	@RequestMapping(value= {"/operators"}, method= {RequestMethod.GET})
	public AjaxResult getList() {
		
		PageHelper.startPage();
		Operator o = new Operator();
		o.setAgentId(ContextThread.get().getAgentId());
	    Page<Operator>  pages =operatorMapper.queryPage(o);
		
		return new AjaxResult().success(pages);
	}
	/**
	 *  个人中心修改资料
	 * @param pojo
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/me"}, method= {RequestMethod.POST})
	public AjaxResult modify(@RequestBody Operator pojo) {
		String operatorId = ContextThread.get().getOperatorId();
		Operator o = operatorMapper.selectByPrimaryKey(operatorId);
		o.setName(pojo.getName());
		o.setPhone(pojo.getPhone());
		o.setUpdateDate(new Date());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		operatorMapper.updateByPrimaryKeySelective(o);
		return new AjaxResult().success();
	}
	/**
	 * 员工修改密码
	 * @param obj
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/password"}, method= {RequestMethod.POST})
	public AjaxResult updatePassword(@RequestBody JSONObject obj) {
		String password=obj.getString("password");
		String enPass= Crypt.enAes(password);
		String operatorId = ContextThread.get().getOperatorId();
		Operator o = operatorMapper.selectByPrimaryKey(operatorId);
		o.setPassword(enPass);
		o=operatorMapper.validateLogin(o);
		if(o==null) {
			return new AjaxResult().failure("您输入的原密码错误");
		}
		
		String password1=obj.getString("password1");
		String password2=obj.getString("password2");
		if(password1==null||!password1.equals(password2)) {
			return new AjaxResult().failure("您两次输入的新密码不一致");
		}
		o.setPassword(Crypt.enAes(password1));
		o.setUpdateDate(new Date());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		operatorMapper.updateByPrimaryKeySelective(o);
		operatorCaches.refreshRole(o.getId());
		return new AjaxResult().success();
	}
	/**
	 * 员工重置密码
	 * @param obj
	 * @return
	 */
	@Transactional
	@RequestMapping(value= {"/operatorpwd"},method= {RequestMethod.PUT})
	public AjaxResult replacePwd(@RequestBody JSONObject obj) {
		String id=obj.getString("id");
		Operator o = operatorMapper.selectByPrimaryKey(id);
		o.setPassword(Crypt.enAes(Constant.replacePwd));
		operatorMapper.updateByPrimaryKey(o);
		operatorCaches.refreshRole(o.getId());
		return new AjaxResult().success();
	}
}
